package com.fyzk.raceview;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.bean.Race;
import com.fyzk.bean.ZhuiHaoDetail;
import com.fyzk.constants.ConstantsDJ;
import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;


public abstract class RaceShuZiCaiView extends RaceBaseView {

	protected final String TAG = "RaceShuZiCaiView";

	private Context context;

	protected List<String> lotteryIds = new ArrayList<String>();
	public static  List<String> wcodes = new ArrayList<String>();
	public static List<List<BetNum>> betNums = new ArrayList<List<BetNum>>();

//	@Override
	public View getRaceView(Context context, String content, List<Race> races,
			String prizeNum) {
//		// TODO Auto-generated method stub
//		this.context = context;
//		View view = LayoutInflater.from(context).inflate(
//				R.layout.detail_szc_race, null);
//		LinearLayout prize_ll = (LinearLayout) view
//				.findViewById(R.id.detail_szc_prize_ll);
//		TextView prize_tv = (TextView) view
//				.findViewById(R.id.detail_szc_prize_tv);
//		TextView fangAn_tv = (TextView) view
//				.findViewById(R.id.detail_szc_fangan_tv);
//		LinearLayout parent_ll = (LinearLayout) view
//				.findViewById(R.id.detail_szc_ll);
//		prize_tv.setValue(prizeNum);
//
//		lotteryIds.clear();
//		wcodes.clear();
//		betNums.clear();
//		splitContent(content, prizeNum, codeFilter);
//
//		allNum = betNums.size();
//
//		for (int i = 0; i < betNums.size(); i++) {
//			if (i < num) {
//				View itemView = addItemView(context, i);
//				if (itemView != null) {
//					parent_ll.addView(itemView);
//				}
//			} else if (i == num) {
//				TextView textView = new TextView(context);
//				// textView.setValue(
//				//
//				textView.setPadding(0, 20, 0, 20);
//				textView.setValue("查看全部方案内容");
//				textView.setOnClickListener(new OnClickListener() {
//
//					@Override
//					public void onClick(View v) {
//						// TODO Auto-generated method stub
//						Intent intent = new Intent(getContext(), MoreCodeActivity.class);
//						getContext().startActivity(intent);
//					}
//				});
//				textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
//				textView.setMovementMethod(LinkMovementMethod.getInstance());
//				textView.setTextColor(context.getResources().getColor(
//						R.color.top_bg));
//				textView.setGravity(Gravity.CENTER);
//				textView.setBackgroundColor(Color.WHITE);
//				parent_ll.addView(textView);
//			}
//		}
//		if (prizeNum != null && prizeNum.endsWith("hm")) {
//			prize_ll.setVisibility(View.GONE);
//			fangAn_tv.setVisibility(View.GONE);
//		}
		return new TextView(context);
	}
	
	public Context getContext() {
		return context;
	}

	private View addItemView(Context context, int i) {
		View view = LayoutInflater.from(context).inflate(
				R.layout.detail_szc_race_item, null);
		TextView wcode_tv = (TextView) view
				.findViewById(R.id.detail_szc_wcode_tv);
		GridView gv = (GridView) view.findViewById(R.id.detail_szc_gv);

		TextView num_tv = (TextView) view.findViewById(R.id.detail_szc_num_tv);
		wcode_tv.setText(ConstantsDJ.wcode(wcodes.get(i)));
		gv.setVisibility(View.GONE);
		List<BetNum> betNs = betNums.get(i);
		String codes = "";
		for (BetNum betNum : betNs) {
			String code = betNum.betNum;
			if (betNum.isPrizeNum) {
				codes += "<font color=\"#ff0000\">" + code + "</font>" + " ";
			} else {
				codes += "<font color=\"#000000\">" + code + "</font>" + " ";
			}
		}

		String htmlStr = "<html><body>" + codes + "</body></html>";
		num_tv.setText(Html.fromHtml(htmlStr));

		return view;
	}

	/**
	 * 拆分 投注内容
	 * 
	 * @param content
	 */
	protected abstract void splitContent(String content, String prizeNum);

	protected void splitContent(String content, String prizeNum, int codeFilter) {
		splitContent(content, prizeNum);
	}

	class GridViewAdapter extends BaseAdapter {

		private List<BetNum> betNums;

		public GridViewAdapter(List<BetNum> betNums) {
			this.betNums = betNums;
		}

		@Override
		public int getCount() {
			if (betNums == null) {
				return 0;
			}
			return betNums.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			BetNum betNum = betNums.get(position);
			convertView = LayoutInflater.from(context).inflate(
					R.layout.detail_szc_gv_item, null);

			TextView tv = (TextView) convertView
					.findViewById(R.id.detail_szc_gv_tv);
			tv.setText(betNum.betNum + "");
			if (betNum.isPrizeNum) {
				if (betNum.type == BetNum.TYPE_BULE) {
					tv.setTextColor(Color.BLUE);
				} else {
					tv.setTextColor(Color.RED);
				}
			}

			return convertView;
		}

	}

	@Override
	public View getZhuiHaoRaceView(Context context, String content,
			List<ZhuiHaoDetail> detail) {
		this.context = context;
		View view = LayoutInflater.from(context).inflate(
				R.layout.detail_zhuihao_race, null);
		LinearLayout zhuiHao_ll = (LinearLayout) view
				.findViewById(R.id.detail_zhuihao_ll);

		for (int i = 0; i < detail.size(); i++) {
			View itemView = addZhuiHaoItemView(context, content, detail.get(i));
			if (itemView != null) {
				zhuiHao_ll.addView(itemView);
			}
		}
		return view;
	}

	private View addZhuiHaoItemView(Context context, String content,
			ZhuiHaoDetail detail) {
		View view = LayoutInflater.from(context).inflate(
				R.layout.detail_zhuihao_race_item, null);
		LinearLayout item_ll = (LinearLayout) view
				.findViewById(R.id.detail_zhuihao_item_ll);
		TextView phase_tv = (TextView) view
				.findViewById(R.id.detail_zhuihao_phase_tv);
		TextView multiple_tv = (TextView) view
				.findViewById(R.id.detail_zhuihao_multiple_tv);
		TextView money_tv = (TextView) view
				.findViewById(R.id.detail_zhuihao_money_tv);
		TextView prizeAmount_tv = (TextView) view
				.findViewById(R.id.detail_zhuihao_prizemoney_tv);
		LinearLayout prizeAmount_ll = (LinearLayout) view
				.findViewById(R.id.detail_zhuihao_prizemoney_ll);
		TextView status_tv = (TextView) view
				.findViewById(R.id.detail_zhuihao_state_tv);
		final LinearLayout xiangXi_ll = (LinearLayout) view
				.findViewById(R.id.detail_zhuihao_xiangxi_ll);
		TextView prizeNum_tv = (TextView) view
				.findViewById(R.id.detail_zhuihao_prizenum_tv);
		final LinearLayout xiangXiContent_ll = (LinearLayout) view
				.findViewById(R.id.detail_zhuihao_xiangxi_content_ll);

		phase_tv.setText("第 " + detail.phase + " 期");
		multiple_tv.setText(detail.multiple + "倍");
		money_tv.setText(detail.amount + "米");
		prizeAmount_tv.setText(detail.prizeAmount);
		boolean isPrize = false;
		if (detail.prizeAmount == null || detail.prizeAmount.equals("")
				|| detail.prizeAmount.equals("0")
				|| detail.prizeAmount.equals("null")) {
			prizeAmount_ll.setVisibility(View.INVISIBLE);
		} else {
			isPrize = true;
			prizeAmount_ll.setVisibility(View.VISIBLE);
			status_tv.setTextColor(Color.RED);
		}

		String status = ConstantsDJ.zhuiHaoItem_state(detail.status,
				detail.prizeNum, isPrize);
		status_tv.setText(status);

		lotteryIds.clear();
		wcodes.clear();
		betNums.clear();
		splitContent(content, detail.prizeNum);
		prizeNum_tv.setText(detail.prizeNum);
		for (int i = 0; i < betNums.size(); i++) {
			View itemView = addItemView(context, i);
			if (itemView != null) {
				xiangXiContent_ll.addView(itemView);
			}
		}

		item_ll.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (xiangXi_ll.getVisibility() == View.VISIBLE) {
					xiangXi_ll.setVisibility(View.GONE);
				} else {
					xiangXi_ll.setVisibility(View.VISIBLE);
				}
			}
		});

		return view;
	}

}
